<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//require_once("Loader.php");

session_start();

class LoaderFormss
{
    public static function getLoader()
    {
        $loader = $_SESSION['loader'];
        if($loader === null)
        {
            $loader = new Loader();
            $_SESSION['loader'] = $loader;
        }
        return $loader;
    }

    private static $Records = null;
    public static function load()
    {
        $loader = LoaderForm::getLoader();

        $fileName = "TM Scenario 130621.csv";
        $file = fopen($fileName, "r");
        $fileContent = fread($file, filesize($fileName));
        fclose($file);
        $excelContent = split("\r\n", $fileContent);
        $columns = array();
        $records = array();
        for($i=0; $i<sizeof($excelContent); $i++)
        {
            if($i == 0)
            {
                //$columns = split(",", $excelContent[$i]);
                $columns = split(",", $excelContent[$i]);
                for($j=0; $j<sizeof($columns); $j++)
                    $columns[$j] = trim($columns[$j]);
                continue;
            }

            $row = split(",", trim($excelContent[$i]));
            if($row[0] === "")continue;

            $record = array();
            for($j=0; $j<sizeof($row); $j++)
            {
                $record[$columns[$j]] = $row[$j];
            }
            $records[] = $record;

            $loader->getWarehouse()->addDeliveryOrder(new DeliveryOrder($record['Delivery'], $record['Cust Code'], $record['Street'], $record['Weight'], $record['Volume'], $record['Longitude'], $record['Latitude']));
        }
        LoaderForm::$Records = $records;

        /*$loader->getWarehouse()->addDeliveryOrder(new DeliveryOrder("A", 10, 5, 107.02207546, -6.17767511));
        $loader->getWarehouse()->addDeliveryOrder(new DeliveryOrder("AA", 10, 5, 107.0962145, -6.24928696));
        $loader->getWarehouse()->addDeliveryOrder(new DeliveryOrder("AAA", 10, 5, 107.08754437, -6.25793724));
        $loader->getWarehouse()->addDeliveryOrder(new DeliveryOrder("B",10, 15, 107.09630523, -6.24807987));
        $loader->getWarehouse()->addDeliveryOrder(new DeliveryOrder("BA", 35, 5, 107.09611915, -6.24987126));
        $loader->getWarehouse()->addDeliveryOrder(new DeliveryOrder("C", 5, 6, 107.09611915, -6.24987126));*/
        /*echo sizeof($loader->getWarehouse()->DeliveryOrderList)."<br />".
            sizeof(LoaderForm::getLoader()->getWarehouse()->DeliveryOrderList);*/
    }

    public static function generate()
    {
        $loader = LoaderForm::getLoader();

        $loader->generate();

        $carList = "[";
        foreach($loader->getWarehouse()->CarList as $index => $val)
        {
            $carList .= $val->toString().", ";
        }
        $carList = trim($carList, ", ");
        $carList .= "]";
        return $carList;
    }

    public static function getDeliveryOrderList()
    {
        $loader = LoaderForm::getLoader();

        $doList = "[";
        foreach($loader->getWarehouse()->DeliveryOrderList as $index => $val)
        {
            $doList .= $val->toString().", ";
        }
        $doList = trim($doList, ", ");
        $doList .= "]";
        return $doList;
    }

    public static function getCarGroupList()
    {
        $loader = LoaderForm::getLoader();

        $carGroupList = "[";
        foreach($loader->getWarehouse()->CarList as $index => $val)
        {
            $carGroupList .= $val->toString().", ";
        }
        $carGroupList = trim($carGroupList, ", ");
        $carGroupList .= "]";
        return $carGroupList;
    }

    public static function getPathList()
    {
        $loader = LoaderForm::getLoader();

        $pathList = "[";
        foreach($loader->getWarehouse()->CarList as $index => $val)
        {
            foreach($val->Path as $idx2 => $val2)
            {
                $obj = "{".
                    "CarID : '".$val->CarID."',".
                    "MaxDistance : '".$val->MaxDistance."',".
                    "MaxWeight : '".$val->MaxWeight."',".
                    "MaxVolume : '".$val->MaxVolume."',".
                    "Weight : '".$val->Weight."',".
                    "Volume : '".$val->Volume."',".
                    "DOID : '".$val2->ID."',".
                    "DONum : '".$val2->DONum."',".
                    "CustCode : '".$val2->CustCode."',".
                    "Distance : '".$val2->Distance."',".
                    "DOWeight : '".$val2->Weight."',".
                    "DOVolume : '".$val2->Volume."',".
                    "Delivered : '".$val2->Delivered."',".
                    "Longitude : '".$val2->getLongitude()."',".
                    "Latitude : '".$val2->getLatitude()."'}";
                $pathList .= $obj.",";
            }
        }
        $pathList = trim($pathList, ", ");
        $pathList .= "]";
        return $pathList;
    }

    public static function reset()
    {
        $loader = LoaderForm::getLoader();
        $loader->getWarehouse()->reset();
    }
}

/*if($_REQUEST['request'] === NULL)
{
    echo "<pre>";

    print_r(LoaderForm::getLoader()->getWarehouse()->CarList);
    echo "<br />DO List : ";
    print_r(LoaderForm::getLoader()->getWarehouse()->DeliveryOrderList);

    echo "</pre>";
}
else
{*/
    switch($_REQUEST['request'])
    {
        case 'load':
/*          session_destroy();
            session_start();*/
            LoaderForm::load();
            /*echo "Load : <br />";
            print_r(LoaderForm::getLoader()->getWarehouse()->CarList);
            echo "<br /><br />";
            print_r(LoaderForm::getLoader()->getWarehouse()->DeliveryOrderList);*/
            break;
        case 'generate':
            echo LoaderForm::generate();
            break;
        case 'getCarGroupList':
            echo LoaderForm::getCarGroupList();
            /*echo "<pre>";
            print_r(LoaderForm::getLoader()->getWarehouse()->CarList);
            echo "</pre>";*/
            break;
        case 'getDeliveryOrderList':
            echo LoaderForm::getDeliveryOrderList();
            /*echo "<pre>";
            print_r(LoaderForm::getLoader()->getWarehouse()->DeliveryOrderList);
            echo "</pre>";*/
            break;
        case 'getPathList':
            echo LoaderForm::getPathList();
            break;
        case 'reset':
            LoaderForm::reset();
            break;
    }

/*echo "<pre>";

print_r(LoaderForm::getLoader()->getWarehouse()->CarList);
echo "<br />DO List : ";
print_r(LoaderForm::getLoader()->getWarehouse()->DeliveryOrderList);

echo "</pre>";*/

    exit();
//}